import {user} from '@/common/framework';
export default class Http{
	
	/**
	 * 发起 GET 请求 https://uniapp.dcloud.io/api/request/request
	 * @param {String} url
	 * @param {Object} data
	 */
	static get(url,data){
		let userInfo=user.getUserInfo();
		return new Promise((a,b)=>{
			uni.request({
				url:url,
				data:data,
				header:{
					'X-Access-Token':userInfo.token
				},
				method:'GET',
				success(res){
					a(res.data,res.statusCode);
				},
				fail(err){
					console.log(url,JSON.stringify(err));
					b(err);
				}
			})
		});
	}
	
	/**
	 * 发起 POST 请求 https://uniapp.dcloud.io/api/request/request
	 * @param {String} url
	 * @param {Object} data
	 */
	static post(url,data){
		let userInfo=user.getUserInfo();
		return new Promise((a,b)=>{
			uni.request({
				url:url,
				data:data,
				header:{
					'content-type':'application/json',
					'X-Access-Token':userInfo.token
				},
				method:'POST',
				success(res){
					a(res.data,res.statusCode);
				},
				fail(err){
					b(err);
					console.log(JSON.stringify(err));
				}
			})
		});
	}
	
	static postForm(url,data){
		let userInfo=user.getUserInfo();
		return new Promise((a,b)=>{
			uni.request({
				url:url,
				data:data,
				header:{
					'content-type':'application/x-www-form-urlencoded',
					'X-Access-Token':userInfo.token
				},
				method:'POST',
				success(res){
					a(res.data,res.statusCode);
				},
				fail(err){
					b(err);
					console.log(JSON.stringify(err));
				}
			})
		});
	}
	
	static put(url,data){
		let userInfo=user.getUserInfo();
		return new Promise((a,b)=>{
			uni.request({
				url:url,
				data:data,
				header:{
					'content-type':'application/json',
					'X-Access-Token':userInfo.token
				},
				method:'PUT',
				success(res){
					a(res.data,res.statusCode);
				},
				fail(err){
					b(err);
					console.log(JSON.stringify(err));
				}
			})
		});
	}
	
	/**
	 * 上传文件 https://uniapp.dcloud.io/api/request/network-file?id=uploadfile
	 * @param {String} url
	 * @param {Object} data
	 * @param {Function} onSuccess
	 * @param {Function} onFail
	 * @param {Function} onProgressUpdate
	 * @param {String} upfile
	 */
	static upload(option){
		let userInfo=user.getUserInfo();
		let uploadTask  = uni.uploadFile({
			url:option.url,
			// formData:option.data,
			filePath:option.file,
			fileType:option.fileType,
			name:option.name,
			header:{
				// 'content-type':'multipart/form-data',
				'X-Access-Token':userInfo.token
			},
			// method:'POST',
			success(res){
				if(typeof option.onSuccess=="function"){
					option.onSuccess(JSON.parse(res.data),res.statusCode);
				}
			},
			fail(err){
				if(typeof option.onFail=="function"){
					option.onFail(err);
					console.log(JSON.stringify(err));
				}
			}
		});
		uploadTask.onProgressUpdate(res=>{
			if(typeof option.onProgressUpdate=="function"){
				option.onProgressUpdate(res);
			}
		})
		
	}
	
	/**
	 * 下载文件 https://uniapp.dcloud.io/api/request/network-file?id=uploadfile
	 * @param {String} url
	 * @param {Function} onSuccess
	 * @param {Function} onFail
	 * @param {Function} onProgressUpdate
	 */
	static download(url,onSuccess,onFail,onProgressUpdate){
		let downloadTask = uni.downloadFile({
		    url: url,
		    success: (res) => {
		        if(typeof onSuccess=="function"){
		        	onSuccess(res.data,res.statusCode);
		        }
		    },
			fail(err){
				if(typeof onFail=="function"){
					onFail(err);
					console.log(JSON.stringify(err));
				}
			}
		});
		downloadTask.onProgressUpdate(res=>{
			if(typeof onProgressUpdate=="function"){
				onProgressUpdate(res);
			}
		})
	}
}